package com.huika.dealontop.control.myinfo.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hk.app.android.lib.http.VolleyRequestManager;
import com.huika.dealontop.GlobalApp;
import com.huika.dealontop.R;
import com.huika.dealontop.control.base.BaseAct;
import com.huika.dealontop.control.common.SetEnvironmentActivity;
import com.huika.dealontop.control.main.MainActivity;
import com.huika.dealontop.control.myinfo.adapter.SelectLanguageLvAdapter;
import com.huika.dealontop.support.domain.SelectLanguageBean;
import com.huika.dealontop.support.event.SelectLanguageEvent;
import com.huika.dealontop.utils.FileTool;
import com.huika.dealontop.utils.PreferencesHelper;
import com.huika.dealontop.utils.SelectLanguageUtils;
import com.huika.dealontop.utils.VersionManager;
import com.huika.dealontop.views.CommonTitleRightWindow;
import com.huika.dealontop.views.PromptOperatDialog;
import com.huika.dealontop.views.TitleBarHelper;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;
import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * @author chenyuan
 * @description：设置
 * @date 2015年11月16日 上午11:25:04
 */
public class SettingActivity extends BaseAct {

    private static String MESSAGE_CB = "message_cb";

    private TextView cashTV;
    private TextView setting_language_tv;
    private TextView check_version_tv;
    private RelativeLayout setting_clearcash_rl;
    private String selectLang ;
    private CheckBox switch_receive_remind;
    private RelativeLayout rl_title;
    private long mPreClickTime;
    private int mCount;
    private static final int SWITCH_VERSION_CLK_NUM = 10;
    private VersionManager versionManager;
    private CommonTitleRightWindow moreWindow;
    private TitleBarHelper titleBarHelper;
    private ImageView title_right_iv;

    private Dialog updateDialog;



    @Override
    public void setRootView() {
        setContentView(R.layout.activity_setting);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        titleBarHelper=new TitleBarHelper(this, R.string.common_empty, R.string.common_empty, R.string.setting_title);
//        titleBarHelper.setRightImg(new DindanDbHelper().getUnUsedableCountNum() > 0 ? R.mipmap.common_more_has_message_icon : R.mipmap.common_more_icon);
        titleBarHelper.setRightPop();
//        titleBarHelper.setOnRightImgClickListener(this);
        rl_title = (RelativeLayout) findViewById(R.id.rl_title);
        title_right_iv = (ImageView) findViewById(R.id.title_right_iv);
        cashTV = (TextView) findViewById(R.id.tv_cache);
        setting_clearcash_rl = (RelativeLayout) findViewById(R.id.setting_clearcash_rl);
        setting_language_tv = (TextView) findViewById(R.id.setting_language_tv);
        check_version_tv = (TextView) findViewById(R.id.check_version_tv);
        switch_receive_remind = (CheckBox) findViewById(R.id.switch_receive_remind);
        switch_receive_remind.setChecked(PreferencesHelper.getInstance().getBoolean(MESSAGE_CB, true));
        cashTV.setText(FileTool.getCacheSizeString(FileTool.getCacheSize()));
        setting_clearcash_rl.setOnClickListener(this);
        setting_language_tv.setOnClickListener(this);
        check_version_tv.setOnClickListener(this);
        rl_title.setOnClickListener(this);
        versionManager = new VersionManager(this);
        selectLang = SelectLanguageUtils.getCurrentLanguageCode(this);
        switch_receive_remind.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    PreferencesHelper.getInstance().setBoolean(MESSAGE_CB, true);
                } else {
                    PreferencesHelper.getInstance().setBoolean(MESSAGE_CB, false);
                }
            }
        });

    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()) {
            case R.id.title_right_iv:
//                moreWindow = new CommonTitleRightWindow(aty);
//                moreWindow.showWindow(title_right_iv);
                break;
            case R.id.setting_clearcash_rl: // 清空缓存
                final PromptOperatDialog dialog = new PromptOperatDialog(SettingActivity.this, null);
                dialog.setTitile(getString(R.string.settingf_clear_cache_tips));
                dialog.setCancelClick(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        File volleyCacheDir = VolleyRequestManager.getCacheDir();
                        FileTool.clear(volleyCacheDir);
                        File cacheDirectory = StorageUtils.getCacheDirectory(SettingActivity.this);
                        FileTool.clear(cacheDirectory);
                        File cacheFile = GlobalApp.getInstance().getCacheDir();
                        FileTool.clear(cacheFile);
                        File cacheDir = StorageUtils.getOwnCacheDirectory(GlobalApp.getInstance(), GlobalApp.getInstance().getPackageName() + File.separator + "cache/images");
                        FileTool.clear(cacheDir);
                        File cDir = new File("/storage/sdcard0/Android/data/com.huika.dealontop/cache");
                        FileTool.clear(cDir);
                        cashTV.setText(FileTool.getCacheSizeString(FileTool.getCacheSize()));
                        showToastMsg(getResources().getString(R.string.setting_clear_success));
                        dialog.dismiss();
                    }
                });
                dialog.setConfirmClick(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();


                break;

            case R.id.setting_language_tv:
                View view = View.inflate(SettingActivity.this, R.layout.select_language_dialog, null);
                final ListView langLv = (ListView) view.findViewById(R.id.lang_lv);
                final SelectLanguageLvAdapter lvAdapter = new SelectLanguageLvAdapter(SettingActivity.this);
                langLv.setAdapter(lvAdapter);
                lvAdapter.setGroup(setLangData());
                langLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        int relPosition = position - langLv.getHeaderViewsCount();
                        if (relPosition >= 0 && relPosition < lvAdapter.getCount()) {
                            switch (position) {
                                case 0:
                                    lvAdapter.setIcon(SelectLanguageUtils.LANG_ZH);
                                    selectLang = SelectLanguageUtils.LANG_ZH;
                                    break;
                                case 1:
                                    lvAdapter.setIcon(SelectLanguageUtils.LANG_TH);
                                    selectLang = SelectLanguageUtils.LANG_TH;
                                    break;
                                case 2:
                                    lvAdapter.setIcon(SelectLanguageUtils.LANG_EN);
                                    selectLang = SelectLanguageUtils.LANG_EN;
                                    break;
                            }
                        }
                    }
                });
                final PromptOperatDialog dialog_lang = new PromptOperatDialog(SettingActivity.this, view);
                dialog_lang.setTitleGone();
                dialog_lang.setCenterLineGone();
                dialog_lang.setCancelBtnGone();
                dialog_lang.getConfirmBtn().setText(getString(R.string.register_confirm));
                dialog_lang.setConfirmClick(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog_lang.dismiss();
                        switchLanguage();
                    }
                });
                dialog_lang.show();
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        lvAdapter.setIcon(SelectLanguageUtils.getCurrentLanguageCode(SettingActivity.this));
                    }
                }, 50);
                break;
            case R.id.rl_title:
                long now = System.currentTimeMillis();
                if (now - mPreClickTime < 300) {
                    mCount++;
                } else {
                    mCount = 1;
                }
                mPreClickTime = now;
                if (mCount >= SWITCH_VERSION_CLK_NUM) {
                    Intent intent = new Intent(SettingActivity.this, SetEnvironmentActivity.class);
                    startActivity(intent);
                }
                break;

            case R.id.check_version_tv:
                versionManager.checkVersion(false, true, new Handler());
//                showActivity(this, CommonSubbranch.class);
                break;
        }

    }


    private void switchLanguage() {
        PreferencesHelper.setString(SelectLanguageUtils.PRE_LANG, selectLang);
        SelectLanguageUtils.setLanguage(this);
        EventBus.getDefault().post(new SelectLanguageEvent(""));
        finish();
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    private Handler mHandler = new Handler();

    private ArrayList<SelectLanguageBean> setLangData() {
        ArrayList<SelectLanguageBean> datas = new ArrayList<SelectLanguageBean>();
        SelectLanguageBean bean1 = new SelectLanguageBean();
        bean1.setLang("中文简体");
        bean1.setCountryCode(SelectLanguageUtils.LANG_ZH);
        datas.add(bean1);
        SelectLanguageBean bean2 = new SelectLanguageBean();
        bean2.setLang("ไทย");
        bean2.setCountryCode(SelectLanguageUtils.LANG_TH);
        datas.add(bean2);
        SelectLanguageBean bean3 = new SelectLanguageBean();
        bean3.setLang("English");
        bean3.setCountryCode(SelectLanguageUtils.LANG_EN);
        datas.add(bean3);
        return datas;
    }

}
